package org.spring.springboot.controller;

import org.json.JSONArray;
import org.json.JSONObject;
import org.spring.springboot.domain.Symbol;
import org.spring.springboot.service.ISymbolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/symbol") // 这里定义了总的路径 "/api"
public class SymbolController {

    @Autowired
    private ISymbolService symbolService;

    @PostMapping("/getSystemSymol")
    public List<Object> getSystemSymol(@RequestBody Map<String, Object> jsonMap) {
        List<Symbol> list= symbolService.selectAll("system");
        System.out.println(list.size());

        JSONArray arr = new JSONArray();
        for (Symbol p:list) {
            JSONObject obj = p.toJson();
            arr.put(obj);
        }
        return arr.toList();
    }

    @PostMapping("/getCustSymbol")
    public List<Object> getCustSymbol(@RequestBody Map<String, Object> jsonMap) {
        JSONObject jsonObject = new JSONObject(jsonMap);
        String userid = jsonObject.getString("userid");

        List<Symbol> list= symbolService.selectAll(userid);
        System.out.println(list.size());

        JSONArray arr = new JSONArray();
        for (Symbol p:list) {
            JSONObject obj = p.toJson();
            arr.put(obj);
        }
        return arr.toList();
    }

    @PostMapping("/save")
    public Map<String, Object> save(@RequestBody Map<String, Object> jsonMap) {
        JSONObject jsonObject = new JSONObject(jsonMap);
        String uuid = jsonObject.getString("uuid");

        Symbol fileObj = new Symbol();
        fileObj.setUuid(jsonObject.getString("uuid"));
        fileObj.setTypeID(jsonObject.getInt("typeID"));
        fileObj.setName(jsonObject.getString("name"));
        fileObj.setIconURL(jsonObject.getString("iconURL"));

        fileObj.setAutoLabel(jsonObject.getInt("autoLabel"));

        fileObj.setMoldActionType(jsonObject.getInt("moldActionType"));
        fileObj.setUnitFixedFlag(jsonObject.getInt("unitFixedFlag"));
        JSONObject dataObj = jsonObject.getJSONObject("data");
        fileObj.setData(dataObj.toString());

        String userid = jsonObject.getString("userid");
        String category = jsonObject.getString("category");
        fileObj.setCategory(category);
        fileObj.setUserid(userid);

        int count = symbolService.getCount(userid,category);
        fileObj.setDisorder(count+1);
        fileObj.setDlftLabPos(jsonObject.getString("dlftLabPos"));

        symbolService.save(fileObj);

        JSONObject retJson = new JSONObject();
        retJson.put("uuid",uuid);
        return retJson.toMap();
    }

    @PostMapping("/updateFileData")
    public Map<String, Object> updateFileData(@RequestBody Map<String, Object> jsonMap) {
        JSONObject jsonObject = new JSONObject(jsonMap);
        String uuid = jsonObject.getString("uuid");

        JSONObject dataObj = jsonObject.getJSONObject("data");
        String fileData = dataObj.toString();
        String iconURL = jsonObject.getString("iconURL");

        symbolService.updateData(uuid,fileData,iconURL);

        JSONObject retJson = new JSONObject();
        retJson.put("uuid",uuid);
        return retJson.toMap();
    }

    @PostMapping("/updateProfile")
    public Map<String, Object> update(@RequestBody Map<String, Object> jsonMap) {
        JSONObject jsonObject = new JSONObject(jsonMap);
        String uuid = jsonObject.getString("uuid");
        String parent = jsonObject.getString("parent");
        String name = jsonObject.getString("name");
        String iconURL = jsonObject.getString("iconURL");

        symbolService.updateProfile(uuid,name,iconURL);

        JSONObject retJson = new JSONObject();
        retJson.put("uuid",uuid);
        return retJson.toMap();
    }

    @PostMapping("/delete")
    public Map<String, Object> delete(@RequestBody Map<String, Object> jsonMap) {
        JSONObject jsonObject = new JSONObject(jsonMap);
        String uuid = jsonObject.getString("uuid");
        String name = jsonObject.getString("name");

        symbolService.delete(uuid);

        JSONObject retJson = new JSONObject();
        retJson.put("uuid",uuid);
        return retJson.toMap();
    }



    @PostMapping("/getSymbolByClass")
    public List<Object> getSymbolByClass(@RequestBody Map<String, Object> jsonMap) {
        JSONObject jsonObject = new JSONObject(jsonMap);
        String userid = "";
        if (jsonObject.has("userid")){
            userid = jsonObject.getString("userid");
        }
        else{
            return null;
        }
        String category = jsonObject.getString("category");

        List<Symbol> list= symbolService.getSymbolByClass(userid,category);
        System.out.println(list.size());

        JSONArray arr = new JSONArray();
        for (Symbol p:list) {
            JSONObject obj = p.toJson();
            arr.put(obj);
        }
        return arr.toList();
    }

}
